import requests,openpyxl

class getSth:

    def getZ(self):

        self.commentid = ''

        song_info_list = []

        url = 'https://c.y.qq.com/soso/fcgi-bin/client_search_cp'

        params = {

            'ct': '24',
            'qqmusic_ver': '1298',
            'new_json': '1',
            'remoteplace': 'txt.yqq.song',
            'searchid': '61190558053343224',
            't': '0',
            'aqqr': '1',
            'cr': '1',
            'catZhida': '1',
            'lossless': '0',
            'flag_qc': '0',
            'p': '1',
            'n': '10',
            'w': '%E5%91%A8%E6%9D%B0%E4%BC%A6',
            'g_tk': '5381',
            'loginUin': "0",
            'format': 'json',
            'inCharset': 'utf-8',
            'outCharset': 'utf-8',
            'notice': '0',
            'platform': 'yqq.json',
            'needNewCode': '0'
        }

        headers = {

            'origin': 'https://y.qq.com',

            'referer': 'https://y.qq.com/portal/search.html',

            'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'

        }

        res = requests.get(url,params=params,headers=headers)

        res_json = res.json()

        song_list = res_json['data']['song']['list']

        for info in song_list:

            song_name = info['name']

            song_time = info['interval']

            song_key = info['mid']

            song_url = 'https://y.qq.com/n/yqq/song/'+song_key+'.html'

            song_info_list.append([song_name,str(song_time)+'秒',song_url])

        return song_info_list



    def saveXLS(self):

        song_info = self.getZ()

        wb = openpyxl.Workbook()

        sheet = wb.active

        sheet.title = '周杰伦歌曲信息'

        sheet['A1'] = '歌曲名称'

        sheet['B1'] = '歌曲时长'

        sheet['C1'] = '歌曲链接'

        for info in song_info:

            sheet.append(info)

        wb.save('周杰伦歌曲信息.xlsx')


a = getSth()
a.saveXLS()